package com.wing.sell.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.exception.BusinessException;
import com.wing.sell.dao.AgentFollowRecordDao;
import com.wing.sell.model.entity.AgentFollowRecord;
import com.wing.sell.model.request.AgentFollowRecordForm;
import com.wing.sell.model.response.AgentFollowRecordVO;
import com.wing.sell.service.AgentFollowRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 销售代理商经销跟进记录-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class AgentFollowRecordServiceImpl extends ServiceImpl<AgentFollowRecordDao, AgentFollowRecord> implements AgentFollowRecordService {

    @Override
    @Transactional
    public void add(AgentFollowRecordForm agentFollowRecordForm) throws Exception{

        //校验

        AgentFollowRecord agentFollowRecord =new AgentFollowRecord();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(AgentFollowRecord.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(agentFollowRecordForm, agentFollowRecord, copyOptions);
        String code=getCode();
        agentFollowRecord.setCode("F"+code);
        agentFollowRecord.setNo(Integer.valueOf(code));
        baseMapper.insert(agentFollowRecord);
    }

    private String getCode(){
        String code="";
        Integer maxNo=baseMapper.getMaxNo();
        if (maxNo==null){
            maxNo=1;
        }else{
            maxNo+=1;
        }
        for (int i=0;i<4-maxNo.toString().length();i++){
            code+="0";
        }
        return code+maxNo.toString();
    }

    @Override
    public List<AgentFollowRecordVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<AgentFollowRecordVO> pageList4app(Page page, Map<String, Object> params) {
        List<AgentFollowRecordVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public AgentFollowRecordVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }


    @Override
    @Transactional
    public void update(AgentFollowRecordForm agentFollowRecordForm) throws Exception {
        AgentFollowRecord agentFollowRecord = this.getById(agentFollowRecordForm.getId());
        if(agentFollowRecord == null){
            throw new BusinessException("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(AgentFollowRecord.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(agentFollowRecordForm, agentFollowRecord, copyOptions);

        this.baseMapper.updateById(agentFollowRecord);
    }
}
